﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TwoPageView3 {
    /// <summary>
    /// FolderTreeViewControl.xaml の相互作用ロジック
    /// </summary>
    public partial class FolderTreeViewControl : UserControl {
        private ITpvDirectoryInfo mSelectedDirectoryInfo;
        private IFolderTreeViewEvent mListener;

        public FolderTreeViewControl() {
            InitializeComponent();
            var drives = new FolderTreeViewRootFolderModel();
            base.DataContext = drives;
        }

        public void ConnectTo(IFolderTreeViewEvent listener) {
            mListener = listener;
        }

        public ITpvDirectoryInfo SelectedDirectoryInfo() {
            return mSelectedDirectoryInfo;
        }

        private void OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) {
            var tv = sender as TreeView;
            var item = tv.SelectedItem as FileSystemTreeViewItemViewModel;
            string path = item.MakePath();
            Console.WriteLine("OnSelectedItemChanged() {0}", path);

            mSelectedDirectoryInfo = FolderPathToDirectoryInfo(path);
            if (null != mListener) {
                mListener.OnFolderTreeViewSelected(mSelectedDirectoryInfo);
            }
            e.Handled = true;
        }

        private ITpvDirectoryInfo FolderPathToDirectoryInfo(string path) {
            return TpvDirectoryInfoFactory.Factory(path);
        }
    }
}
